package q754_reachNumber;

public class Solution_2 {
    /*
    纯数学 O(1)时间复杂
     */
    public int reachNumber(int target) {
        target = Math.abs(target);
        int n = (int) ((-1 + Math.sqrt(1 + 8.0 * target)) / 2);
        int d = target - n * (n + 1) / 2;
        if (d == 0) return n;
        d -= ++n;
        return ((d & 1) == 1 ? n + 1 + n % 2 : n);
    }
}
